Skip to content

Conversation

@iwoplaza
Copy link
Collaborator

@iwoplaza iwoplaza commented Oct 8, 2025

No description provided.

@github-actions
Copy link

github-actions bot commented Oct 8, 2025

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/typegpu@946f45f4e404e7de8b450211cf0557cb8180dda6
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@946f45f4e404e7de8b450211cf0557cb8180dda6
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@946f45f4e404e7de8b450211cf0557cb8180dda6

benchmark
view benchmark

commit
view commit

@iwoplaza iwoplaza changed the title docs: JS Kernels docs: TypeGPU functions Oct 16, 2025
@iwoplaza iwoplaza marked this pull request as ready for review October 29, 2025 15:07
Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually 10/10 docs - I would read them for fun even though I know all those things 🙇


In order to construct a TypeGPU function, you need to start by defining its shell, an object holding only the input and output types.
:::note[WGSL enthusiasts!]
Don't let the JavaScript scare you! TypeGPU functions can be implemented using either WGSL or JS, both being able to call one another.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe discourage over scare? Scare sounds a little condescending to me.

const main = () => {
'use gpu';
// Call from another function
return neighborhood(1.1, 0.5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those numbers are quite carefully chosen I see 😄 I think we should either wrap them in d.f32() or at least explain the dangers of using plain literals (eg: showing the 1.0 case)

EDIT: I just now reached a section where you explain it in depth - maybe we should do a short warning with a reference to that in case people get trigger happy and do not reach that?

To make this all work, we perform a small transformation to functions marked with `'use gpu'`, leaving all other code untouched. Every project's setup is different, and we want to be as non-invasive as possible. The [unplugin-typegpu](/TypeGPU/tooling/unplugin-typegpu) package hooks into existing bundlers and build tools, extracts ASTs from TypeGPU functions and compacts them into our custom format called [tinyest](https://www.npmjs.com/package/tinyest). This metadata is injected into the final JS bundle, then used to efficiently generate equivalent WGSL at runtime.
:::tip
If you wish for all WGSL generation to happen at build time, combine [unplugin-macros](https://github.com/unplugin/unplugin-macros) and our [resolve API](/TypeGPU/fundamentals/resolve).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all WGSL generation at build time is not possible most of the time so we shouldn't sell it like that - maybe all build time known WGSL generation?

Comment on lines +306 to +309
* **Operators** --
JavaScript does not support operator overloading.
This means that, while you can still use operators for numbers,
you have to use supplementary functions from `typegpu/std` (*add, mul, eq, lt, ge...*) for operations involving vectors and matrices, or use a fluent interface (*abc.mul(xyz), ...*)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could mention the //ts-ignore (no @ since of course there is a user with that username) trick for people who like cursed code 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't want people to abuse that and feel like we're endorsing it, as it introduces more problems down the line (result inferred as any, not working properly on the CPU)

Comment on lines +311 to +312
* **Math.\*** --
Utility functions on the `Math` object can't automatically run on the GPU, but can usually be swapped with functions exported from `typegpu/std`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could mention that constants are fine

Comment on lines +53 to +55
"starlight-typedoc": "^0.19.0",
"tinybench": "^3.1.0",
"typedoc": "^0.28.13",
"typedoc-plugin-markdown": "^4.3.0",
"typedoc": "^0.27.9",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason for this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, the newer versions of these packages didn't generate any API Reference docs, but they also didn't fail the nr build command.

@iwoplaza iwoplaza merged commit 12b2d38 into main Oct 29, 2025
3 checks passed
@iwoplaza iwoplaza deleted the docs/js-kernels branch October 29, 2025 17:05
@aleksanderkatan aleksanderkatan restored the docs/js-kernels branch October 30, 2025 14:21
Copy link
Contributor

@aleksanderkatan aleksanderkatan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great docs!
I leave some nits

TypeGPU functions let you define shader logic in a modular and type-safe way.
**TypeGPU functions** let you define shader logic in a modular and type-safe way.
Their signatures are fully visible to TypeScript, enabling tooling and static checks.
Dependencies, including GPU resources or other functions, are resolved automatically, with no duplication or name clashes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependencies are resolved automatically, but the externals are collected automatically only with unplugin & TGSL, do we want to mention it here?

// ^?

// #2) Used to generate WGSL
const wgsl = tgpu.resolve({ externals: { main } });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be a little more explicit here, since this is close to the very beginning of the docs.

Suggested change
const wgsl = tgpu.resolve({ externals: { main } });
const wgsl = tgpu.resolve({ template: "", externals: { main } });

I really think we should add a simplified resolve api btw

```

TypeGPU then propagates those types into the function body and analyses the types returned by the function.
If it cannot unify them into a single type, it will throw an error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

them may be ambiguous here

Suggested change
If it cannot unify them into a single type, it will throw an error.
If it cannot unify the types found in different return statements into a single type, it will throw an error.

Copy link
Collaborator

@cieplypolar cieplypolar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful docs! Easy to read yet comprehensive 🦖

:::

* **Calling other functions** --
Only functions marked with `'use gpu'` can be called from within a shader. An exception to that rule is [`console.log`](/TypeGPU/fundamentals/utils#consolelog), which allows for tracking runtime behavior
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only functions marked with 'use gpu' - what about shelled functions? They don't have to be marked with 'use gpu', however we can call them from within a shader.

return getGradientColor((in.uv[0] + in.uv[1]) / 2);
}`.$uses({ getGradientColor });
```

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should include an example using the TypeGPU function in addition to raw WGSL. When using TypeGPU functions, we need to explicitly name the in parameter, for example: (input) => { ... }.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand what you mean 👀


## Storage and uniform variables

When using TypeGPU for resolution, there is no need to ever define either `var<uniform>` or `var<storage[, address_space]>`, as such definitions are added automatically.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When using TypeGPU for resolution, there is no need to ever define either `var<uniform>` or `var<storage[, access_mode]>`, as such definitions are added automatically.

| `var<uniform>` | `root.createUniform()` |
| `var<storage, read>` | `root.createReadonly()` |
| `var<storage, read_write>` | `root.createMutable()` |

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a comment about as('...'), because we can make buffer a fixed resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants